/**
 * Created by wangguoqing on 2017/11/8.
 */

var i_dh = '1';
//机构下拉框初始化赋值标志位，初始化时不触发onChange事件

//页面加载脚本
$(function () {
    getUserInfo();
    $("#dhs").hide();
});

function reload() {
    location.reload();
}

function logonOut() {
    $.messager.alert("操作提示", "是否要退出本次登录？","confirm", function () {
            location.href = path + "/system/register/logout";
    });
}

function openInform() {
    $('#sysinform').show();
    $('#sysinform').window({
        modal: true,
        width: 700,
        height: 300
    });
    $('#sysinform').window('open');
}

function getUserInfo() {
    document.getElementById("yhm").innerHTML = "&nbsp;&nbsp;" + userInfo.userName;
    $("#orgCode").combobox({
        editable: false,
        disabled: false,
        data: userInfo.orgDtos,
        valueField: 'orgCode',
        textField: 'orgName',
        onChange: function (rec) {
            orgCodeChange();
        },
        onLoadSuccess: function () {
            $(this).combobox('setValue', userInfo.curUserOrgCode);
        }
    });
}


function orgCodeChange() {
    var orgCode = $("#orgCode").combobox("getValue");
    var parameter = new Object();
    parameter.curOrgCode = orgCode;
    $.ajax({
        type: "POST",
        url: path + "/system/register/orgCodeChange",
        dataType: "json",
        data: parameter,
        contentType: "application/x-www-form-urlencoded",
        async: false,
        success: function callback(data) {
            var analyMsg = data;
            if (!(analyMsg.code == "0000")) {
                if (analyMsg.code == "3002") {
                    layer.alert(analyMsg.msg, {closeBtn: 0, shift: 4});
                }
                //layer.alert(analyMsg.msg, {closeBtn: 0, shift: 4 });
            } else {
                closeAllTabs();
                refreshTree(path + '/system/register/iniMenu');//初始化机构树，后台需要添加节点
                refreshDhl();
                var currentTab = $('#tabs').tabs('getSelected');
                RefreshTab(currentTab);
                //$.cookie('curOrgCode', orgCode, {expires: 1});
            }
        }
    });
}
function changeDhl() {
    if (i_dh == '1') {
        $("#dhl").hide();
        $("#dhs").show();
        i_dh = '2';
    } else {
        $("#dhs").hide();
        $("#dhl").show();
        i_dh = '1';
    }
}
function refreshDhl() {
    var params = new Object();
    $.ajax({
        type: "POST",
        url: path + "/system/register/getSubMenu",
        dataType: "json",
        data: params,
        contentType: "application/x-www-form-urlencoded",
        async: false,
        success: function callback(data) {
            var analyMsg = data;
            if (!(analyMsg.code == "0000")) {
                //$.messager.alert("友情提醒", analyMsg.msg, "info", function () {
                //});
                $.alert("default", "info", analyMsg.msg);
            } else {
                var s = '<ul>';
                for (var i = 0; i < analyMsg.returnObject.rows.length; i++) {
                    s = s + '<li><a id = "' + analyMsg.returnObject.rows[i]["menuCode"] + '" href="javascript:create2LevelMenu(';
                    s = s + analyMsg.returnObject.rows[i]["menuCode"] + ')" class="' + analyMsg.returnObject.rows[i]["moduleIcon"];
                    s = s + '"><span id="span' + analyMsg.returnObject.rows[i]["menuCode"] + '">' + analyMsg.returnObject.rows[i]["menuName"] + '</span></a></li>';
                }
                s = s + '</ul>';
                document.getElementById("dhl").innerHTML = s;
                hoverDhl();
            }
        }
    });
}

function reloadTab(subtitle, url, icon, id) {
    var isExist = $("#tabs").tabs("existsById", id);
    if (isExist) {
        $("#tabs").tabs("selectById", id);
        var currentTab = $('#tabs').tabs('getSelected');
        currentTab.panel('setTitle', subtitle);
        $('#tabs').tabs('update', {
            tab: currentTab,
            options: {
                content: createFrame(id, path + url)
            }
        });
    } else {
        $('#tabs').tabs('add', {
            id: id,
            title: subtitle,
            content: createFrame(id, path + url),
            closable: true,
            icon: icon
        });
    }
    lastTabs = $.grep(lastTabs, function (n, i) {
        return n != subtitle;
    });
    lastTabs.push("我的桌面");
    //重新压入，保证 最新的在最上面
    lastTabs.push(subtitle);
    //更新当前
    currentTabTitle = subtitle;
    tabClose();
}

//查看2级菜单
function create2LevelMenu(menuCode) {

    var len = $("#dhl a").size();
    var arr = [];
    for (var index = 0; index < len; index++) {//创建一个数字数组
        arr[index] = index;
    }
    $.each(arr, function (i) {//循环得到不同的id的值
        var idValue = $("#dhl a").eq(i).attr("id");
        if (idValue != '') {
            $("#" + idValue).removeClass("sel");
        }
    });

    $("#" + menuCode).addClass("sel");

    var params = new Object();
    params.menuCode = menuCode;
    $.ajax({
        type: "POST",
        url: path + "/system/register/queryByCode",
        data: params,
        dataType: "json",
        contentType: "application/x-www-form-urlencoded",
        async: false,
        success: function callback(data) {
            var analyMsg = data;
            if (analyMsg.code != "0000") {
                //layer.alert(analyMsg.msg, {closeBtn: 0, shift: 4 });
                if ((analyMsg.code == "9999")) {
                    //$.messager.alert("友情提醒", analyMsg.msg, "info", function () {
                    $.messager.alert("信息", analyMsg.msg, "info", function () {
                        var top = window;
                        while (top != top.parent) {
                            top = top.parent;
                        } //获取当前页面的顶层窗口对象
                        top.location.href = "/system/register/toLogin"; //跳转到登陆页面
                    });
                }
            } else {
                reloadTab(analyMsg.returnObject['menuName'], path + '/system/syswelcome/index_right?menuCode=' + menuCode, '', 99999999);
            }
        },
        error: function (data, textstatus) {
            $.alert("default", "error", data.responseText)
        }
    });
}

function hoverDhl() {
    $("#west").css("overflow", "hidden");
    $("#west").hover(function () {
        $("#west").css("overflow", "");
        $("#west").css("overflow-x", "hidden");
    }, function () {
        $("#west").css("overflow", "hidden");
    });
}

//密码修改
function modifyPassword() {
    layer.open({
        type: 2,
        title: '修改当前密码',
        shadeClose: false,
        shade: 0.5,
        area: ['340px', '220px'],
        content: path + '/system/register/toModify' //iframe的url
    });
}